package com.java2020.online_mall.dao;

import com.java2020.online_mall.model.Goods;
import com.java2020.online_mall.model.GoodsParam;
import com.java2020.online_mall.model.GoodsSpec;
import com.java2020.online_mall.model.GoodsType;
import com.java2020.online_mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackgeName: com.java2020.online_mall.dao
 * @Author: MNHK
 * Date: 2020/5/2 21:43
 * @Version:1.8
 */

public class GoodsDaoImpl implements GoodsDao{

    private QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

    @Override
    public List<GoodsType> getType() {
        List<GoodsType> typeList = null;
        try {
            typeList = queryRunner.query("select * from type", new BeanListHandler<GoodsType>(GoodsType.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return typeList;
    }

    @Override
    public List<Goods> getGoodsByType(String typeId) {
        List<Goods> goodsList = null;
        try {
            goodsList = queryRunner.query("select * from goods where typeId = ?",
                    new BeanListHandler<Goods>(Goods.class),
                    typeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    @Override
    public int addGoods(GoodsParam goodsParam) {
        try {
            queryRunner.update("insert into goods values (null,? ,? ,? ,? ,? ,?)",
                    goodsParam.getImg(),
                    goodsParam.getName(),
                    goodsParam.getPrice(),
                    goodsParam.getTypeId(),
                    goodsParam.getStockNum(),
                    goodsParam.getDesc());
            //此时再取出good的id
            return insertId();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private int insertId() {
        BigInteger number = null;
        try {
            number = queryRunner.query("select last_insert_id()", new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return number.intValue();
    }

    @Override
    public void addSpecList(int goodsId, List<GoodsSpec> specList) {
        for(GoodsSpec goodsSpec : specList){
            try {
                queryRunner.update("insert into spec values (null, ?, ?, ?, ?)",
                        goodsSpec.getSpecName(),
                        goodsSpec.getStockNum(),
                        goodsSpec.getUnitPrice(),
                        goodsId);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int addType(GoodsType type) {
        try {
            queryRunner.update("insert into type (name) values (?) ", type.getName());
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        return 200;
    }

    @Override
    public int deleteGoods(String id) {
        try {
            queryRunner.update("delete from goods where id = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        //删除该商品所包含的spec
        try {
            queryRunner.update("delete from spec where goodsId = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 200;
    }

    @Override
    public int deleteType(String id) {
        try {
            queryRunner.update("delete from type where id = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        return 200;
    }

    @Override
    public  Map<String, Object> getGoodsInfo(String id) {
        Goods goods = null;
        List<GoodsSpec> goodsSpecs = null;
        //取商品信息
        try {
            goods = queryRunner.query("select * from goods where id = ?",
                    new BeanHandler<>(Goods.class),
                    id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //取该商品的各种规格信息
        try {
            goodsSpecs = queryRunner.query("select * from spec where goodsId = ?",
                    new BeanListHandler<>(GoodsSpec.class),
                    id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("goods", goods);
        map.put("goodsSpecs", goodsSpecs);
        return map;
    }

    @Override
    public int addSpec(GoodsSpec spec) {
        try {
            queryRunner.update("insert into spec values (null, ?, ?, ?, ?)",
                    spec.getGoodsId(),
                    spec.getSpecName(),
                    spec.getStockNum(),
                    spec.getUnitPrice());
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        return 200;
    }

    @Override
    public int deleteSpec(GoodsSpec spec) {
        try {
            queryRunner.update("delete from spec where goodsId = ? && specName = ?",
                    spec.getGoodsId(),
                    spec.getSpecName());
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        return 200;
    }

    @Override
    public int updateGoods(GoodsParam goodsParam) {
        try {
            queryRunner.update("update goods set img = ?, name = ?, price = ?, stockNum = ?, typeId = ?, `desc` = ? where id = ?",
                    goodsParam.getImg(),
                    goodsParam.getName(),
                    goodsParam.getPrice(),
                    goodsParam.getStockNum(),
                    goodsParam.getTypeId(),
                    goodsParam.getDesc(),
                    goodsParam.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        //更新spec中数据
        List<GoodsSpec> specList = goodsParam.getSpecList();
        return updateSpec(specList);
    }

    private int updateSpec(List<GoodsSpec> specList) {
        for (GoodsSpec goodsSpec : specList) {
            try {
                queryRunner.update("update spec set specName = ?, stockNum = ?, unitPrice = ? where id = ?",
                        goodsSpec.getSpecName(),
                        goodsSpec.getStockNum(),
                        goodsSpec.getUnitPrice(),
                        goodsSpec.getId());
            } catch (SQLException e) {
                e.printStackTrace();
                return 500;
            }
        }
        return 200;
    }
}
